package com.li.controller;

import com.alibaba.fastjson.JSONObject;
import com.li.domain.UploadFile;
import com.li.domain.User;
import com.li.service.EmployeeRoleService;
import com.li.service.UploadFileService;
import com.li.service.UserService;
import com.li.utlis.EasyExcelUtil;
import com.li.utlis.FileUploadUtil;
import com.li.utlis.R;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {

    @Autowired
    private UserService userService;

    @Autowired
    private UploadFileService uploadFileService;

    @Autowired
    private EmployeeRoleService employeeRoleService;

    /**
     * 查询全部
     * @return
     */
    @RequestMapping("/listUser")
    public List<User> listUser(){
        return userService.list();
    }

    /**
     * 模糊查询
     * @param user
     * @return
     */
    @RequestMapping("/listQueryUser")
    public JSONObject listQueryUser(User user, Integer page, Integer limit){
        List<User> list = userService.listQueryUser(user,page,limit);
        Integer count = userService.listQueryUserCount(user);
        return R.listPage(list,count);
    }

    /**
     * 添加或修改
     * @param user
     * @return
     */
    @PostMapping("/saveOrUpdateUser")
    public JSONObject saveOrUpdataUser(User user){
        try {
            if (user.getId()==null || "".equals(user.getId())){
                userService.addUser(user);
            }else{
                userService.updateUser(user);
            }
        }catch (Exception e){
            return R.getError(user);
        }
        return R.getSuccess();
    }

    /**
     * 根据id查找一条
     * @param userId
     * @return
     */
    @RequestMapping("/editUser/{userId}")
    public User editUser(@PathVariable("userId") String userId){
        return userService.getById(userId);
    }

    /**
     * 删除
     * @param userId
     */
    @GetMapping("/delUser/{userId}")
    public JSONObject delUser(@PathVariable("userId") String userId){
        try {
            userService.delUser(userId);
        }catch (Exception e){
            return R.getError(userId);
        }
        return R.getSuccess();
    }

    /**
     * 批量删除
     * @param user
     */
    @RequestMapping("/delUsers")
    public JSONObject delUsers(User user){
        String []userIds=user.getUserIds();
        try {
            boolean flag=userService.delUsers(userIds);
            if (!flag){
                return R.getError(userIds);
            }
        }catch (Exception e){
            return R.getError(userIds);
        }
        return R.getSuccess();
    }

    /**
     * 批量导入
     * @param uploadFile 导入的文件
     * @param addOrUpdate 是否添加
     * @param req
     * @return 返回结果
     */
    @RequestMapping("/importFileAddUsers")
    public JSONObject importFileAddUsers(MultipartFile uploadFile,Boolean addOrUpdate, HttpServletRequest req){
        try {
            boolean flag = userService.importFileAddUsers(uploadFile, addOrUpdate , req);
            if (!flag){
                return R.getError();
            }
        }catch (Exception e){
            return R.getError();
        }
        return R.getSuccess();
    }

    /**
     * 导出数据
     * @param req
     * @return
     */
    @RequestMapping("/downloadUsers")
    public JSONObject downloadUsers(HttpServletRequest req){
        String path=null;
        try {
            path = userService.downloadUsers(req);
        }catch (Exception e){
            return R.getError();
        }
        return R.successAndData(path);
    }

    /**
     * 头像上传
     * @param uploadFile
     * @param req
     * @return
     */
    @RequestMapping("/uploadAvatar")
    public String uploadAvatar(MultipartFile uploadFile, HttpServletRequest req){
        String path=null;
        try {
            path=userService.uploadAvatar(uploadFile , req);
        }catch (Exception e){
            return null;
        }
        return path;
    }
}
